/*
 * Copyright 2009 Armando Blancas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package tests.taskgraph.tasks.io;

import java.io.IOException;

import junit.framework.Assert;
import junit.framework.JUnit4TestAdapter;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import taskgraph.channels.CharChannel;
import taskgraph.ports.CharInputPort;
import taskgraph.ports.CharOutputPort;
import taskgraph.tasks.io.ReadTextFile;

public class ReadTextFileTest {

	/**
	 * Runs the test suite in this class from the command line.
	 * 
	 * @param args	Arguments are ignored.
	 */
	public static void main(String[] args) {
		org.junit.runner.JUnitCore.runClasses(ReadTextFileTest.class);
	}
	
	/**
	 * Provides compatibility with 3.x versions of JUnit.
	 * 
	 * @return A 3.x-compatible test suite.
	 */
	public static junit.framework.Test suite() {
		return new JUnit4TestAdapter(ReadTextFileTest.class);
	}
	
	private CharOutputPort output;
	
	@Before
	public void setup() {
		CharChannel channel = new CharChannel();
		output = channel.getOutputPort();
	}
	
	@After
	public void cleanup() {
		output.close();
	}
	
    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Test the constructors.                                               | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/
	
	@Test
	public void defaultConstructor() {
		ReadTextFile task = new ReadTextFile();
		Assert.assertNotNull(task);
	}
	
	@Test
	public void defaultInstanceClass() {
		ReadTextFile task = new ReadTextFile();
		Assert.assertEquals(ReadTextFile.class, task.getClass());
	}

	@Test
	public void constructorWithPorts() {
		ReadTextFile task = new ReadTextFile(output, "filename");
		Assert.assertNotNull(task);
	}
	
	@Test
	public void instanceClassWithSize() {
		ReadTextFile task = new ReadTextFile(output, "filename");
		Assert.assertEquals(ReadTextFile.class, task.getClass());
	}

    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Test the run method.                                                 | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/
	
	@Test
	public void testRun() throws IOException {
		String[] lines = {
			"anatomy",
			"duty",
			"animation",
			"applet",
			"integrate",
			"application",
			"argument",
			"security",
			"painless",
			"bolts"
		};
		
		String filename = "tests-src/tests/taskgraph/tasks/io/words.txt";

		CharChannel ch = new CharChannel();
		ReadTextFile task = new ReadTextFile(ch.getOutputPort(), filename);
		CharInputPort input = ch.getInputPort();		
		task.run();

		Assert.assertEquals(lines[0], input.readLine());
		Assert.assertEquals(lines[1], input.readLine());
		Assert.assertEquals(lines[2], input.readLine());
		Assert.assertEquals(lines[3], input.readLine());
		Assert.assertEquals(lines[4], input.readLine());
		Assert.assertEquals(lines[5], input.readLine());
		Assert.assertEquals(lines[6], input.readLine());
		Assert.assertEquals(lines[7], input.readLine());
		Assert.assertEquals(lines[8], input.readLine());
		Assert.assertEquals(lines[9], input.readLine());
		
		input.close();
	}

    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Negative tests for constructors.                                     | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/

	@Test(expected=IllegalArgumentException.class)
	public void constructorWithNullOutputPort() throws IOException {
		new ReadTextFile(null, "pattern");
	}

	@Test(expected=IllegalArgumentException.class)
	public void constructorWithNullFilename() throws IOException {
		new ReadTextFile(output, null);
	}

    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Negative tests.                                                      | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/

}
